﻿using System;
using InsideSalesApiClient;

namespace InsideSalesApiClientDemo
{
    class Program
    {
        /// <summary>
        /// Builds the inside sales add lead options.
        /// </summary>
        /// <param name="dialerInitiativeId">The dialer_initiative_id.</param>
        /// <param name="inboundInitiativeId">The inbound_initiative_id.</param>
        /// <returns></returns>
        private static InsideSalesAddLeadOptions BuildInsideSalesAddLeadOptions(int dialerInitiativeId, int inboundInitiativeId)
        {
            var addLeadOptions = dialerInitiativeId != 0
                                     ? new InsideSalesAddLeadOptions
                                     {
                                         do_not_notify = true,
                                         call_now = true,
                                         dup_basis = new[] { "phone" },
                                         time_frame = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd"),
                                         relate_to_dup = true,
                                         dialer_initiative_id = dialerInitiativeId,
                                     }
                                     : inboundInitiativeId != 0
                                           ? new InsideSalesAddLeadOptions
                                           {
                                               do_not_notify = true,
                                               call_now = true,
                                               dup_basis = new[] { "phone" },
                                               time_frame = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd"),
                                               relate_to_dup = true,
                                               inbound_initiative_id = inboundInitiativeId
                                           }
                                           : null;
            return addLeadOptions;
        }

        /// <summary>
        /// Builds the inside sales lead.
        /// </summary>
        /// <param name="campaignId">The campaign id.</param>
        /// <returns></returns>
        private static InsideSalesLead BuildInsideSalesLead(int campaignId)
        {
            var leadToAdd = new InsideSalesLead
            {
                external_id = "1",
                campaign_id = campaignId,
                website = "www.example.com",
                customFields_36 = "LeadGenForm",
                customFields_34 = "127.0.0.1",
                customFields_30 = "Customer",
                first_name = "FirstName",
                last_name = "LastName",
                email = "email@example.com",
                phone = "5555555555",
                other_phone = string.Empty,
                customFields_18 = "AdWord Campaign",
                customFields_28 = "Ad Group",
                customFields_24 = "Medium",
                customFields_20 = "Search Engine",
                customFields_26 = "Keywords"
            };
            return leadToAdd;
        }

        static void Main()
        {
            const int campaignId = 12;         // from insidesales.com campaign id's
            var leadToAdd = BuildInsideSalesLead(campaignId);

            const int initiativeId = 0;        // 
            const int inboundInitiativeId = 4; // from insidesales.com initiative id's
            var addLeadOptions = BuildInsideSalesAddLeadOptions(initiativeId, inboundInitiativeId);

            var insideSalesApiService = new InsideSalesApiService();
            if (insideSalesApiService.IsAuthenticated)
            {
                var dialerLeadId = insideSalesApiService.AddLead(leadToAdd, addLeadOptions);
                Console.Write(dialerLeadId);
            }

            Console.ReadKey();
        }
    }
}
